package com.duoec.magichour.service.impl;

import com.duoec.magichour.dto.response.MessageDetailDto;
import com.duoec.magichour.dto.response.MessageListItemDto;
import com.duoec.magichour.dto.response.UserSimpleInfoDto;
import com.duoec.magichour.mapper.MessageMapper;
import com.duoec.magichour.pojo.Message;
import com.duoec.magichour.service.MessageService;
import com.duoec.magichour.service.UserService;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by ycoe on 17/6/26.
 */
@Service
public class MessageServiceImpl implements MessageService {
    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private UserService userService;

    @Value("${system.avatar}")
    private String systemAvatar;

    @Value("${system.nickname}")
    private String systemNickname;

    @Override
    public List<MessageListItemDto> listMessage(long userId, int messageStatus, int pageNo, int pageSize) {
        List<MessageListItemDto> messageListVo = Lists.newArrayList();
        List<Message> messageList = messageMapper.listMessage(userId, messageStatus, (pageNo - 1) * pageSize, pageSize);
        if (messageList == null || messageList.isEmpty()) {
            return messageListVo;
        }

        Set<Long> userIds = Sets.newHashSet();
        messageList
                .stream()
                .filter(message -> message.getSenderId() > 0)
                .forEach(message -> userIds.add(message.getReceiverId()));

        //获取用户基本信息
        Map<Long, UserSimpleInfoDto> userMap = userService.getUserSimpleInfoMap(Lists.newArrayList(userIds));
        messageList.forEach(message -> {
            MessageListItemDto item = new MessageListItemDto();
            item.setMessageId(message.getId());
            item.setMessageTitle(message.getTitle());

            Long senderId = message.getSenderId();
            if (senderId == null || senderId.equals(0)) {
                //系统
                item.setSenderAvatar(systemAvatar);
                item.setSenderNickname(systemNickname);
            } else {
                UserSimpleInfoDto usi = userMap.get(senderId);
                if (usi != null) {
                    item.setSenderAvatar(usi.getAvatar());
                    item.setSenderNickname(usi.getNickname());
                }
            }

            messageListVo.add(item);
        });

        return messageListVo;
    }

    @Override
    public void read(long userId, List<Long> messageIds) {
        messageMapper.updateMessageStatus(messageIds, 1);
    }

    @Override
    public void delete(long userId, List<Long> messageIds) {
        messageMapper.updateMessageStatus(messageIds, -1);
    }

    @Override
    public MessageDetailDto getDetail(long messageId) {
        Message message = messageMapper.selectByPrimaryKey(messageId);
        if (message == null) {
            return null;
        }
        MessageDetailDto detail = new MessageDetailDto();
        BeanUtils.copyProperties(message, detail);

        List<Long> userIds = Lists.newArrayList();
        Long senderId = message.getSenderId();
        Long receiverId = message.getReceiverId();
        if (senderId != null && senderId > 0) {
            userIds.add(senderId);
        }
        if (receiverId != null && receiverId > 0) {
            userIds.add(receiverId);
        }
        Map<Long, UserSimpleInfoDto> userMap = userService.getUserSimpleInfoMap(userIds);
        UserSimpleInfoDto usi = userMap.get(senderId);
        if (usi != null) {
            detail.setSenderNickname(usi.getNickname());
            detail.setSenderAvatar(usi.getAvatar());
        } else {
            detail.setSenderNickname(systemNickname);
            detail.setSenderAvatar(systemAvatar);
        }

        usi = userMap.get(receiverId);
        if (usi != null) {
            detail.setReceiverNickname(usi.getNickname());
            detail.setReceiverAvatar(usi.getAvatar());
        } else {
            detail.setReceiverNickname(systemNickname);
            detail.setReceiverAvatar(systemAvatar);
        }
        return detail;
    }
}
